package org.lanqiao.servelet;

import org.lanqiao.entity.Train;
import org.lanqiao.service.Impl.TrainServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet(name = "TrainServlet", value = "/train")
public class TrainServlet extends HttpServlet {
    private TrainServiceImpl service=new TrainServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取调用方法名
        req.setCharacterEncoding("utf-8");
        String type=req.getParameter("type");
        try {
            //通过反射获取方法
            Method method=this.getClass().getDeclaredMethod(type,HttpServletRequest.class,HttpServletResponse.class);
            method.setAccessible(true);
            try {
                //调用这个方法
                method.invoke(this,req,resp);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }

    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Train> list=new TrainServiceImpl().findAllTrain();
        req.setAttribute("list",list);
        req.getRequestDispatcher("index.jsp").forward(req,resp);
}
}
